package com.auxgroup.smarthome.job;

import com.auxgroup.smarthome.utils.common.LOG;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by fju on 2017/8/28.
 * 睡眠DIY定时任务
 */
@Configuration
public class AppSleepDiyScheduler {
    @Resource
    private ZookeeperRegistryCenter regCenter;
//    @Resource
//    private JobEventConfiguration jobEventConfiguration;

    @Bean(initMethod = "init")
    public JobScheduler appSleepDiyJobScheduler(final AppSleepDiyJob simpleJob,
                                           @Value("${appSleepDiyJob.cron}") final String cron,
                                           @Value("${appSleepDiyJob.shardingTotalCount}") final int shardingTotalCount,
                                           @Value("${appSleepDiyJob.shardingItemParameters}") final String shardingItemParameters) {
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("睡眠Diy-schedule",
                cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build();
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, simpleJob.getClass().getCanonicalName());
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build();
        LOG.info(this,liteJobConfiguration.toString());
        return new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration);
    }
}
